#include <htc.h> 
#include <stdio.h>
#include "timing.h"

volatile near unsigned char newSecond;
volatile near unsigned int newmillisecond;

void delayMs(unsigned int x) { 
	unsigned long start_millis = 0;
	unsigned long end_millis = 0;

	//limited to 999 milliseconds
	if(x > 999) 
	{
		x = 999;
	}

	start_millis = newmillisecond;  
	end_millis = start_millis + x;

	//end_millis needs to be limited to a full second
	if (end_millis > 999)
	{
		end_millis = end_millis - 999;
	}

	//if the counter wraps around then we count until the counter is less than the start
	// or in other words until we overflow and then count up until the counter is greater than 
	// end otherwise a normal count
	if (end_millis < start_millis)
	{
		//adjustment for handling wrapping around
		while(newmillisecond > start_millis)
		{
			continue;
		} 		
	}
	
	//gotta do this whether there is a wraparound or not
	while(newmillisecond < end_millis)
	{
		continue;
	}				
}

void delaySec(unsigned char x){
	unsigned long start_secs = 0;
	unsigned long end_secs = 0;

	//limited to 59 seconds
	if(x > 59) 
	{
		x = 59;
	}

	start_secs = newSecond;  
	end_secs = start_secs + x;
	
	//end_secs needs to be limited to a full minute
	if (end_secs > 59)
	{
		end_secs = end_secs - 59;
	}

	//if the counter wraps around then we count until the counter is less than the start
	// or in other words until we overflow and then count up until the counter is greater than 
	// end otherwise a normal count
	if (end_secs < start_secs)
	{
		//adjustment for handling wrapping around
		while(newSecond > start_secs)
		{
			continue;
		} 	
	}

	//gotta do this whether there is a wraparound or not
	while(newSecond < end_secs)
	{
		continue;
	}				
}


static void interrupt
isr(void)			
{
	// ********** timer 0 interrupt
	if((T0IE) && (T0IF)) 
	{	
		//frequency is 976.5625hz or 1 pulse every 1.02msec
		
		newmillisecond++;  //a millisecond has accumulated
			
		if(newmillisecond > 999)
		{
			newSecond++;	// a second has accumulated
			if (newSecond > 59)
			{
				newSecond = 0;
			}
		
			newmillisecond = 0; //reset the milliseconds
		}
				
		T0IF = 0;		// clear the interrupt flag
	}

	// ********** timer 2 interrupt
	if((TMR2IE) && (TMR2IF)) 
	{			
		timing = 1;		//let routines know that the timer expired
		TMR2IF = 0;		// clear the interrupt flag
	}
}

void init_interrupts(void){

	// timer0 clock source set to internal clock
	T0CS = 0; 	

	// prescaler is assigned to timer0
	PSA = 0; 	

	// timer 0 prescaler set to 1:4
	PS2 = 0;	
	PS1 = 0;
	PS0 = 1;

	//timer 2 code - used for IR LED
	//postscale set to 10
	TOUTPS3 = 1;
	TOUTPS2 = 0;
	TOUTPS1 = 0;
	TOUTPS0 = 1;

	//timer 2 prescale set to 16 for now
	T2CKPS1 = 1;
	T2CKPS0 = 0;

	//timer 2 preset set for 40 ms for now
	PR2 = SEQGAP;

	//timer 2 is turned on
	TMR2ON = 1;

	//timer 2 - turn on the interrupt as needed
	TMR2IE = 0;
	TMR2IF = 0;
	
	// enable peripheral interrupts and global interrupts
	PEIE = 1;	
	GIE =  1; 	

	// timer 0 interrupt enable
	T0IE = 1;  
}

